import gradio as gr
import requests

# 请将此处的 API_KEY 替换为你自己的 DeepSeek API 密钥
API_KEY = "sk-8ff211f2358e4eb9b181013765debfb5"
API_URL = "https://api.deepseek.com/v1/chat/completions"

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {API_KEY}"
}

def ask_deepseek(question):
    data = {
        "model": "deepseek-chat",
        "messages": [
            {"role": "user", "content": question}
        ]
    }
    try:
        response = requests.post(API_URL, headers=headers, json=data)
        response.raise_for_status()
        result = response.json()
        answer = result["choices"][0]["message"]["content"]
        return answer
    except requests.exceptions.RequestException as e:
        return f"请求出错: {e}"
    except (KeyError, IndexError):
        return "无法解析响应数据"

# 创建 Gradio 界面
iface = gr.Interface(
    fn=ask_deepseek,
    inputs=gr.Textbox(label="请输入你的问题"),
    outputs=gr.Textbox(label="回答"),
    title="DeepSeek 问答系统",
    description="使用 DeepSeek API 进行问答"
)

# 启动 Gradio 界面
if __name__ == "__main__":
    iface.launch()